package com.plan.car.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.plan.car.entity.Dept;
import com.plan.car.entity.Item;
import com.plan.car.entity.Role;
import com.plan.car.entity.User;
import com.plan.car.entity.dto.ItemDTO;
import com.plan.car.entity.vo.ItemVO;
import com.plan.car.service.DeptService;
import com.plan.car.service.ItemService;
import com.plan.car.service.RoleService;
import com.plan.car.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/item")
@Api(tags = "项目管理")
public class ItemController {
    @Resource
    private ItemService itemService;
    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;
    @Resource
    private DeptService deptService;

    @PostMapping("/list")
    @ApiOperation("查询项目列表接口")
    public SaResult getItemList(@RequestBody ItemDTO itemDTO){
        // 判断登录状态，登录才可查询
        if(StpUtil.isLogin()){
            long userId = StpUtil.getLoginIdAsLong();
            User loginUser = userService.getById(userId);
            Role loginRole = roleService.getById(loginUser.getRoleId());
            String roleName = loginRole.getRoleName();
            System.out.println(itemDTO);
            // 超级管理员查看全部任务
            if(roleName.equals("超级管理员")){
                LambdaQueryWrapper<Item> itemLambdaQueryWrapper = new LambdaQueryWrapper<>();
                // 设置 项目名称查询
                if(ObjectUtil.isNotEmpty(itemDTO.getName())){
                    itemLambdaQueryWrapper.like(Item::getName, itemDTO.getName());
                }
                // 设置 项目类型查询
                if(ObjectUtil.isNotEmpty(itemDTO.getBusinessType())){
                    itemLambdaQueryWrapper.like(Item::getBusinessType, itemDTO.getBusinessType());
                }
                // 设置项目编号查询
                if(ObjectUtil.isNotEmpty(itemDTO.getBusinessNumber())){
                    itemLambdaQueryWrapper.like(Item::getBusinessNumber, itemDTO.getBusinessNumber());
                }
                //设置项目负责人工号查询
                if(ObjectUtil.isNotEmpty(itemDTO.getUsername())){
                    itemLambdaQueryWrapper.le(Item::getUsername, itemDTO.getUsername());
                }
                // 设置项目开始时间查询
                if(ObjectUtil.isNotEmpty(itemDTO.getStartTime())){
                    itemLambdaQueryWrapper.ge(Item::getStartTime, itemDTO.getStartTime());
                }
                // 设置项目结束时间查询
                if(ObjectUtil.isNotEmpty(itemDTO.getEndTime())){
                    itemLambdaQueryWrapper.le(Item::getEndTime, itemDTO.getEndTime());
                }
                 //设置部门查询
                if(ObjectUtil.isNotEmpty(itemDTO.getDeptId())){
                    itemLambdaQueryWrapper.eq(Item::getDeptId, itemDTO.getDeptId());
                }
                List<Item> itemList = itemService.list(itemLambdaQueryWrapper);
                List<ItemVO> itemVOList = new ArrayList<>();
                if(!ObjectUtil.isEmpty(itemList)){
                    for(Item item:itemList){
                        ItemVO itemVO = new ItemVO();
                        BeanUtils.copyProperties(item, itemVO);
                        Dept dept = deptService.getById(item.getDeptId());
                        itemVO.setDeptName(dept.getDeptName());
                        User user = userService.getById(item.getCreateBy());
                        itemVO.setCreateByName(user.getName());
                        itemVOList.add(itemVO);
                    }
                    return SaResult.data(itemVOList);
                } else {
                    return SaResult.error("项目列表为空");
                }
            } else {
                // 部门管理员可查看本人所创建的任务
                LambdaQueryWrapper<Item> itemLambdaQueryWrapper = new LambdaQueryWrapper<>();
                itemLambdaQueryWrapper.eq(Item::getCreateBy,userId);
                // 设置 项目名称查询
                if(ObjectUtil.isNotEmpty(itemDTO.getName())){
                    itemLambdaQueryWrapper.like(Item::getName, itemDTO.getName());
                }
                // 设置 项目类型查询
                if(ObjectUtil.isNotEmpty(itemDTO.getBusinessType())){
                    itemLambdaQueryWrapper.like(Item::getBusinessType, itemDTO.getBusinessType());
                }
                // 设置 项目编号查询
                if(ObjectUtil.isNotEmpty(itemDTO.getBusinessNumber())){
                    itemLambdaQueryWrapper.like(Item::getBusinessNumber, itemDTO.getBusinessNumber());
                }
                // 设置项目开始时间查询
                if(ObjectUtil.isNotEmpty(itemDTO.getStartTime())){
                    itemLambdaQueryWrapper.ge(Item::getStartTime, itemDTO.getStartTime());
                }
                // 设置项目结束时间查询
                if(ObjectUtil.isNotEmpty(itemDTO.getEndTime())){
                    itemLambdaQueryWrapper.le(Item::getEndTime, itemDTO.getEndTime());
                }
                //设置部门查询
                if(ObjectUtil.isNotEmpty(itemDTO.getDeptId())){
                    itemLambdaQueryWrapper.eq(Item::getDeptId, itemDTO.getDeptId());
                }
                //设置项目负责人工号查询
                if(ObjectUtil.isNotEmpty(itemDTO.getUsername())){
                    itemLambdaQueryWrapper.le(Item::getUsername, itemDTO.getUsername());
                }

                List<Item> itemList = itemService.list(itemLambdaQueryWrapper);
                List<ItemVO> itemVOList = new ArrayList<>();
                if(!ObjectUtil.isEmpty(itemList)){
                    for(Item item:itemList){
                        ItemVO itemVO = new ItemVO();
                        BeanUtils.copyProperties(item, itemVO);
                        Dept dept = deptService.getById(item.getDeptId());
                        itemVO.setDeptName(dept.getDeptName());
                        User user = userService.getById(item.getCreateBy());
                        itemVO.setCreateByName(user.getName());
                        itemVOList.add(itemVO);
                    }
                    return SaResult.data(itemVOList);
                } else {
                    return SaResult.error("项目列表为空");
                }
            }
        } else {
            return SaResult.error("当前未处于登录状态");
        }
    }
    @ApiOperation("新增项目接口")
    @PostMapping("/add")
    public SaResult addItem(@RequestBody ItemDTO itemDTO){
        if(StpUtil.isLogin()){
            // 用户信息
            long userId = StpUtil.getLoginIdAsLong();
            User loginUser = userService.getById(userId);
            // 新建任务信息
            Item item = new Item();
            BeanUtils.copyProperties(itemDTO,item);
            // 部门信息默认为登录用户归属部门
            item.setDeptId(loginUser.getDeptId());
            item.setCreateBy(userId);
            item.setCreateTime(new Date());
            // 新增
            try {
                itemService.save(item);
                return SaResult.ok("新增项目成功");
            }catch (Exception e){
                return SaResult.error("内部错误，请重试");
            }
        } else {
            return SaResult.error("当前未处于登录状态");
        }
    }

    @ApiOperation("删除项目接口")
    @PostMapping("/delete")
    public SaResult deleteItem(@RequestBody ItemDTO itemDTO){
        if(StpUtil.isLogin()){
            long userId = StpUtil.getLoginIdAsLong();
            User loginUser = userService.getById(userId);
            Role loginRole = roleService.getById(loginUser.getRoleId());
            String roleName = loginRole.getRoleName();
            if(roleName.equals("超级管理员")){
                try {
                    itemService.removeById(itemDTO.getProjectId());
                    return SaResult.ok("删除项目成功");
                }catch (Exception e){
                    return SaResult.error("删除项目失败");
                }
            } else {
                return SaResult.error("无超级管理员权限");
            }
        } else {
            return SaResult.error("当前未处于登录状态");
        }
    }

    @ApiOperation("修改项目信息接口")
    @PostMapping("/edit")
    public SaResult editDept(@RequestBody ItemDTO itemDTO){
        if(StpUtil.isLogin()){
            long userId = StpUtil.getLoginIdAsLong();
            User loginUser = userService.getById(userId);
            Role loginRole = roleService.getById(loginUser.getRoleId());
            String roleName = loginRole.getRoleName();
            if(roleName.equals("超级管理员")){
                try{
                    Item item = new Item();
                    BeanUtils.copyProperties(itemDTO,item);
                    item.setUpdateBy(userId);
                    item.setUpdateTime(new Date());
                    itemService.updateById(item);
                    return SaResult.ok("修改项目信息成功");
                }catch (Exception e){
                    return SaResult.error("项目信息更新失败");
                }
            } else {
                return SaResult.error("无超级管理员权限");
            }
        } else {
            return SaResult.error("当前未处于登录状态");
        }
    }

    @ApiOperation("查询任务信息接口")
    @PostMapping("/select")
    public SaResult selectRole(@RequestBody ItemDTO itemDTO){
        if(StpUtil.isLogin()){
            try {
                Item item = itemService.getById(itemDTO.getProjectId());
                ItemVO itemVO = new ItemVO();
                BeanUtils.copyProperties(item, itemVO);
                User user = userService.getById(item.getCreateBy());
                Dept dept = deptService.getById(item.getDeptId());
                itemVO.setCreateByName(user.getName());
                itemVO.setDeptName(dept.getDeptName());
                return SaResult.data(itemVO);
            }catch (NullPointerException e){
                return SaResult.error("项目不存在");
            }
        } else {
            return SaResult.error("当前未处于登录状态");
        }
    }
}
